﻿#include "pch.h"
#include "Settings.h"
#include "Settings.g.cpp"

#define nameof(x) L#x

namespace winrt::NovelReader::implementation
{
	Settings::Settings()
	{
		this->SetDefaultValue(nameof(FontSize), 20.0);
		this->SetDefaultValue(nameof(WhiteLines), 1);
		this->SetDefaultValue(nameof(FilePath), winrt::hstring{});
		this->SetDefaultValue(nameof(ThemeIndex), -1);
		this->SetDefaultValue(nameof(ChapterIndex), 0);
		this->SetDefaultValue(nameof(FontName), winrt::hstring{L"Font_SimHei"});
		this->SetDefaultValue(nameof(AutoTurnPageSpeed), 30);
		this->SetDefaultValue(nameof(LastReadingPosition), 0.0); 

		m_localSettings = winrt::Windows::Storage::ApplicationData::Current().LocalSettings();

		if (m_localSettings.Values().Size() != m_defaultSettings.size()) {
			m_localSettings.Values().Clear();
			for (auto& [k, v] : m_defaultSettings) {
				m_localSettings.Values().Insert(k, v);
			}
		}
	}

	double Settings::FontSize()
	{
		return this->GetProperty<double>(nameof(FontSize));
	}
	void Settings::FontSize(double value)
	{
		this->SetProperty(nameof(FontSize), value);
	}


	int32_t Settings::WhiteLines()
	{
		return this->GetProperty<int32_t>(nameof(WhiteLines));
	}
	void Settings::WhiteLines(int32_t value)
	{
		this->SetProperty(nameof(WhiteLines), value);
	}


	hstring Settings::FilePath()
	{
		return this->GetProperty<hstring>(nameof(FilePath));
	}
	void Settings::FilePath(hstring const& value)
	{
		this->SetProperty(nameof(FilePath), value);
	}


	int32_t Settings::ThemeIndex()
	{
		return this->GetProperty<int32_t>(nameof(ThemeIndex));
	}
	void Settings::ThemeIndex(int32_t value)
	{
		this->SetProperty(nameof(ThemeIndex), value);
	}


	int32_t Settings::ChapterIndex()
	{
		return this->GetProperty<int32_t>(nameof(ChapterIndex));
	}
	void Settings::ChapterIndex(int32_t value)
	{
		this->SetProperty(nameof(ChapterIndex), value);
	}


	hstring Settings::FontName()
	{
		return this->GetProperty<hstring>(nameof(FontName));
	}
	void Settings::FontName(hstring const& value)
	{
		this->SetProperty(nameof(FontName), value);
	}

	int32_t Settings::AutoTurnPageSpeed()
	{
		return this->GetProperty<int32_t>(nameof(AutoTurnPageSpeed)); 
	}

	void Settings::AutoTurnPageSpeed(int32_t value)
	{
		this->SetProperty(nameof(AutoTurnPageSpeed), value); 
	}

	double Settings::LastReadingPosition()
	{
		return this->GetProperty<double>(nameof(LastReadingPosition)); 
	}

	void Settings::LastReadingPosition(double value)
	{
		this->SetProperty(nameof(LastReadingPosition), value); 
	}

	winrt::event_token Settings::PropertyChanged(winrt::Microsoft::UI::Xaml::Data::PropertyChangedEventHandler const& handler)
	{
		return m_propertyChanged.add(handler);
	}

	void Settings::PropertyChanged(winrt::event_token const& token) noexcept
	{
		m_propertyChanged.remove(token);
	}


	winrt::Windows::Foundation::IInspectable Settings::CreateObject(int32_t value)
	{
		return winrt::Windows::Foundation::PropertyValue::CreateInt32(value); 
	}

	winrt::Windows::Foundation::IInspectable Settings::CreateObject(winrt::hstring const& value)
	{
		return winrt::Windows::Foundation::PropertyValue::CreateString(value);
	}

	winrt::Windows::Foundation::IInspectable Settings::CreateObject(double value)
	{
		return winrt::Windows::Foundation::PropertyValue::CreateDouble(value); 
	}

	winrt::Windows::Foundation::IInspectable Settings::CreateObject(uint32_t value)
	{
		return winrt::Windows::Foundation::PropertyValue::CreateUInt32(value); 
	}
}
